<?php

namespace App\Http\Controllers\Network;

use App\Http\Controllers\Controller;
use App\Models\AdminUserModel;
use App\Models\yb\GoodsModel;
use App\Models\yb\GoodsOrderDetailModel;
use App\Models\yb\GoodsOrderModel;
use App\Models\yb\NetworkCreditRecordModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class StatisticsController extends Controller
{

    /**
     * Get a JWT via given credentials.
     *  统计管理
     * @return \Illuminate\Http\JsonResponse
     */
    public function Detail(Request $request)
    {
        Log::info(json_encode($request->post()));
        $openid = $request->post('openid');

        $user_data = AdminUserModel::where(['openid' => $openid])->first();

        if (empty($user_data) || empty($openid)) {
            return response()->json(['code' => '401', 'msg' => '请登录']);
        }

        $type = $request->post('type',1);

        $screen_data = $request->post('screen_data');

        if($type == 1){
            if(empty($screen_data)){
                $screen_data = date('Y-m-d',time());
            }
            $data['sales_order_number'] = GoodsOrderModel::where(['network_id'=>$user_data['id']])->where('created_at','like',"%".$screen_data."%")->count();
            $data['sales_order_amount'] = GoodsOrderModel::where(['network_id'=>$user_data['id']])->where('created_at','like',"%".$screen_data."%")->sum('total_price');;
        }elseif($type == 2){
            if(empty($screen_data)){
                $start_time = date('Y-m-01', strtotime('-1 month'));
                $end_time = date('Y-m-t', strtotime('-1 month'));
            }else{
                $screen_data_array = explode("|",$screen_data);
                $start_time = $screen_data_array[0];
                $end_time = $screen_data_array[1];
            }
            $data['sales_order_number'] = GoodsOrderModel::where(['network_id'=>$user_data['id']])->where('created_at','>=',$start_time)->where('created_at','=<',$end_time)->count();
            $data['sales_order_amount'] = GoodsOrderModel::where(['network_id'=>$user_data['id']])->where('created_at','>=',$start_time)->where('created_at','=<',$end_time)->sum('total_price');;
            $data['settlement_date_start'] = $start_time;
            $data['settlement_date_end'] = $end_time;
        }elseif($type == 3){
            $data['sales_order_number'] = GoodsOrderModel::where(['network_id'=>$user_data['id']])->where('created_at','like',"%".$screen_data."%")->count();
            $data['sales_order_amount'] = GoodsOrderModel::where(['network_id'=>$user_data['id']])->where('created_at','like',"%".$screen_data."%")->sum('total_price');;
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }


     /**
     * Get a JWT via given credentials.
     *  统计列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function Listing(Request $request)
    {
        $openid = $request->post('openid');

        $user_data = AdminUserModel::where(['openid' => $openid])->first();

        if (empty($user_data) || empty($openid)) {
            return response()->json(['code' => '401', 'msg' => '请登录']);
        }

        $page = $request->post('page', 1);

        $take = $request->post('take', 10);

        $type = $request->post('type',1);

        $screen_data = $request->post('screen_data');

        if($type == 1){
            if(empty($screen_data)){
                $screen_data = date('Y-m-d',time());
            }
            $data = GoodsOrderDetailModel::where(['network_id'=>$user_data['id']])->where('created_at','like',"%".$screen_data."%")->orderBy('created_at','desc')->skip(($page - 1) * $take)->take($take)->get();
        }elseif($type == 2){
            if(empty($screen_data)){
                $start_time = date('Y-m-01', strtotime('-1 month'));
                $end_time = date('Y-m-t', strtotime('-1 month'));
            }else{
                $screen_data_array = explode("|",$screen_data);
                $start_time = $screen_data_array[0];
                $end_time = $screen_data_array[1];
            }
            $data = GoodsOrderDetailModel::where(['network_id'=>$user_data['id']])->where('created_at','>=',$start_time)->where('created_at','=<',$end_time)->orderBy('created_at','desc')->skip(($page - 1) * $take)->take($take)->get();
        }elseif($type == 3){
            $data = GoodsOrderDetailModel::where(['network_id'=>$user_data['id']])->where('created_at','like',"%".$screen_data."%")->orderBy('created_at','desc')->skip(($page - 1) * $take)->take($take)->get();
        }
        foreach($data as $key=>$val){
            $goods_data = GoodsModel::where(['id'=>$val['goods_id']])->select(['goods_title'])->first();
            $data[$key]['goods_title'] = $goods_data['goods_title'];
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }

       /**
     * Get a JWT via given credentials.
     *  配送费明细
     * @return \Illuminate\Http\JsonResponse
     */
    public function DeliveryFeeRecord(Request $request)
    {
        $openid = $request->post('openid');

        $user_data = AdminUserModel::where(['openid' => $openid])->first();

        if (empty($user_data) || empty($openid)) {
            return response()->json(['code' => '401', 'msg' => '请登录']);
        }

        $page = $request->post('page', 1);

        $take = $request->post('take', 10);

        $data = NetworkCreditRecordModel::whereIn('type',['3','4'])->orderBy('created_at','desc')->skip(($page - 1) * $take)->take($take)->get();

        $list['record'] = $data;
        $list['distribution_price'] = $user_data['distribution_price'];

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$list]);

    }


   /**
     * Get a JWT via given credentials.
     * 网点空桶明细
     * @return \Illuminate\Http\JsonResponse
     */
    public function BarrelDepositRecord(Request $request)
    {
         $openid = $request->post('openid');

        $user_data = AdminUserModel::where(['openid' => $openid])->first();

        if (empty($user_data) || empty($openid)) {
            return response()->json(['code' => '401', 'msg' => '请登录']);
        }

        $page = $request->post('page', 1);

        $take = $request->post('take', 10);

        $data = NetworkCreditRecordModel::whereIn('type',['5','6'])->orderBy('created_at','desc')->skip(($page - 1) * $take)->take($take)->get();

        $list['record'] = $data;
        $list['barrels_loaded_number'] = NetworkCreditRecordModel::where(['type'=>5])->sum('number');
        $list['barrels_loaded_amount'] = NetworkCreditRecordModel::where(['type'=>5])->sum('amount');
        $list['barrels_returned_number'] = NetworkCreditRecordModel::where(['type'=>6])->sum('number');
        $list['barrels_returned_amount'] = NetworkCreditRecordModel::where(['type'=>6])->sum('amount');

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$list]);

    }





}
